package router

import (
	"github.com/gin-gonic/gin"
	"go-iot-project/pkg/curd"
	"net/http"
)

func catchError(ctx *gin.Context) {
	defer func() {
		if err := recover(); err != nil {
			switch err.(type) {
			case error:
				curd.Error(ctx, err.(error))
			case string:
				curd.Fail(ctx, err.(string))
			default:
				ctx.JSON(http.StatusOK, gin.H{"error": err})
			}
		}
	}()

	ctx.Next()

	// TODO 内容如果为空，返回404
}

// RegisterRouters 注册路由
func RegisterRouters(router *gin.RouterGroup) {
	router.Use(catchError)

	infoRouter(router.Group("/info"))

	redisRouter(router.Group("/redis"))
}
